#!/usr/bin/perl -w

######################################################
# file : perl/diarysearch.pl
# Author : Anil K Kainikara (anilkumar911@gmail.com)
# Search the diary
#
# This code is licenced under the GPL.
#
######################################################

use Time::Local 'timelocal_nocheck';
use Getopt::Long;
use DBI;
use strict;

my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

my @patterns;
my $start_date="";
my $end_date="";
my $showfullsearch=0;
my $justpattern=0;

my $db = DBI->connect("dbi:SQLite:$ENV{HOME}/.local/share/powerdiary2.db", "", "",
{RaiseError => 1, AutoCommit => 1});

sub main
{
   parsesearchargs(@_);
   if($justpattern == 1){ $showfullsearch = 1;}
   search(@ARGV);
}
   
sub usage()
{
   print "diarysearch.pl -p <pattern> [-s <start_date>] [-e <end_date>] [-f] [-P]"
}

sub parsesearchargs
{
  Getopt::Long::Configure ('bundling');
  GetOptions('p=s'=> \@patterns, 's=s'=> \$start_date,'e=s'=> \$end_date, 'f' => \$showfullsearch, 'P' => \$justpattern);
}

sub search()
{
  $start_date =~ s/\/([0-9])\//\/0$1\//;
  $start_date =~ s/\/([0-9])$/\/0$1/;

  $end_date =~ s/\/([0-9])\//\/0$1\//;
  $end_date =~ s/\/([0-9])$/\/0$1/;

  my $sth;
  if (($start_date ne "") && ($end_date ne ""))
  {
    $sth = $db->prepare("SELECT date, content  FROM  entries  where date >= '$start_date' and date <= '$end_date' order by date");
  }
  elsif ("$start_date" ne "")
  {
    $sth = $db->prepare("SELECT date, content  FROM  entries  where date >= '$start_date' order by date");
  }
  elsif ($end_date ne ""){
    $sth = $db->prepare("SELECT date, content  FROM  entries  where date <= '$end_date' order by date");
  } else {
    $sth = $db->prepare("SELECT date, content  FROM  entries order by date");
  }

  my $rv = $sth->execute();
  my $day=0;
  my $month=0;
  my $year=0;
  my $content="";
  my $date="";

  my $searchpattern="";
  my $pattern="";

  for $pattern (@patterns){
  if($searchpattern ne ""){
      $searchpattern = $searchpattern . "|" . $pattern;
    } else {
      $searchpattern = $pattern;
    }
  }

  while ( ($date,$content) =  $sth->fetchrow_array() )
  {
    my $day_of_week = get_day($date);
    if( $showfullsearch == 1 ){
      if( $content =~ m/($searchpattern)/si ){
        print "\n#########################################################\n";
        print "$date($day_of_week)\n";
        print "#########################################################\n";
        if( $justpattern == 1 ){
          print "$1\n";
        } else {
          print "$content\n";
        }
      }
    } else {
      my $dateprinted=0;
      my $line="";
      while ( $content =~ m/(.*)/g )
      {
         $line=$1;
         if( $1 =~ m/$searchpattern/i ){
           if( $dateprinted != 1 ){
             print "\n#########################################################\n";
             print "$date($day_of_week)\n";
             print "#########################################################\n";
             $dateprinted = 1;
           }
           print "$line \n";
         }
       }
     }
  }
}

sub get_day {
my $date = shift || return(0);
my ($year,$mon,$mday) = $date =~ /(\d+)\/(\d+)\/(\d+)/;
my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
my $day = (localtime($epochtime))[6];
return $weekday[$day];
}

main(@ARGV);
